package com.lz.football.module.activity.qzone
{
	import com.common.utils.DisplayObjectUtil;
	import com.common.utils.Reflection;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.LoginEvent;
	import com.liangzhong.events.ModuleEvent;
	import com.liangzhong.message.http.JsClient;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.activity.qzone.QzoneVo;
	import com.lz.football.module.activity.IQzoneView;
	
	import common.item.ComGoodsItem;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import lz.controls.Alert;
	import lz.controls.core.TitleWindow;

	/**
	 *黄钻页面 
	 * @author lake
	 * 
	 */	
	public class QzoneView extends TitleWindow implements IQzoneView
	{
		/**
		 *条目列表 
		 * 
		 */		
		private var itemList :Array = [];
		/**
		 * 领奖
		 */		
		private var getBtn 	:SimpleButton;
		/**
		 *领取标识 
		 */		
		private var getFlag	:MovieClip;
		/**
		 * 开通黄钻
		 */		
		private var openBtn 	:SimpleButton;
		/**
		 * 开通年钻
		 */		
		private var openYearBtn :SimpleButton;
		
		public function QzoneView()
		{
			super();
		}
		/**
		 *初始化 
		 * 
		 */	
		public function initUI(view:Object=null):void
		{
			_symbolSkin = Reflection.createInstance("com.football.skin.qzoneview");
			_symbolSkin.cacheAsBitmap = true;
			getBtn = _symbolSkin.getBtn;
			openBtn = _symbolSkin.openBtn;
			openYearBtn = _symbolSkin.openYearBtn;
			getFlag = _symbolSkin.getFlag;
			getFlag.visible = false;
			getBtn.visible = false;
			//getBtn.mouseEnabled = false;
			//初始化宽高
			width = _symbolSkin.width;
			height = _symbolSkin.height;
			//初始化UI界面控件
			windowBg = _symbolSkin.windowBg;//背景框
			windowBg.mouseChildren = false;
			//设置可拖动
			dragEnabled = true;
			this.addChild(_symbolSkin);
			closeBtn = Reflection.createInstance("com.lz.commonUI.closeBtn");
			this.addChild(closeBtn);
			closeBtn.x = 545;
			closeBtn.y = 30;
			//设置关闭按钮
			setCloseBtn(closeBtn);
			//add event
			addEvtListener();
			//初始化奖励
			var item :ComGoodsItem;
			var i :int = 0;
			var awards :Array = AppData.getInstance().activityDatemanager.qzoneVo.awards;
			for each(var obp :Object in awards)
			{
				item = new ComGoodsItem();
				item.initUI();
				item.data = AppData.getInstance().goodsManager.getGoodsByTypeAndId(int(obp.type),int(obp.id));
				item.y = 147;
				item.x = 164 + i * 81;
				_symbolSkin.addChild(item);
				itemList.push(item);
				i ++ ;
			}
			setDataInfo();
		}
		/**
		 *注册事件 
		 * 
		 */	
		public function addEvtListener():void
		{
			openBtn.addEventListener(MouseEvent.CLICK,openHuangHandler);
			openYearBtn.addEventListener(MouseEvent.CLICK,openHuangHandler);
			getBtn.addEventListener(MouseEvent.CLICK,onRewardHandler);
			AppData.getInstance().activityDatemanager.qzoneVo.addEventListener(Event.CHANGE,dataChangeHandler);
		}
		/**
		 * onCloseHandler
		 * @param e
		 * 
		 */		
		override protected function onCloseHandler(e :MouseEvent):void
		{
			this.dispatchEvent(new ModuleEvent(ModuleEvent.REMOVE_MODULE));
		}
		/**
		 *开通黄砖 
		 * @param e
		 * 
		 */		
		private function openHuangHandler(e :MouseEvent):void
		{
			switch(e.target)
			{
				case openBtn:
				{
					JsClient.app_tobemember(1);
					break;
				}
				case openYearBtn:
				{
					JsClient.app_tobemember(2);
					break;
				}
			}
		}
		/**
		 *领取奖励 
		 * @param e
		 * 
		 */		
		private function onRewardHandler(e :MouseEvent):void
		{
			var qzoneInfo :QzoneVo = AppData.getInstance().activityDatemanager.qzoneVo;
			if(qzoneInfo.isvip == 0 && qzoneInfo.isyearvip == 0)
			{
				Alert.showErrorTip("您还不是黄钻用户");
				return ;
			}
			AppEventDispatcher.getInstance().dispatchEvent(new LoginEvent(LoginEvent.USER_GET_QZONE_VIP_AWARD));
		}
		/**
		 *数据变化 
		 * @param e
		 */		
		private function dataChangeHandler(e :Event):void
		{
			setDataInfo();
		}
		/**
		 *设置数据 
		 */		
		private function setDataInfo():void
		{
			var qzoneInfo :QzoneVo = AppData.getInstance().activityDatemanager.qzoneVo;
			if(qzoneInfo.status == 1)//已领取
			{
				getBtn.visible = false;
				getFlag.visible = true;
			}
			else
			{
				getBtn.visible = true;
				getFlag.visible = false;
			}
		}
		/**
		 *删除事件 
		 * 
		 */	
		public function removeEvtListener():void
		{
			openBtn.removeEventListener(MouseEvent.CLICK,openHuangHandler);
			openYearBtn.removeEventListener(MouseEvent.CLICK,openHuangHandler);
			getBtn.removeEventListener(MouseEvent.CLICK,onRewardHandler);
			closeBtn.removeEventListener(MouseEvent.CLICK,onCloseHandler);
			AppData.getInstance().activityDatemanager.qzoneVo.removeEventListener(Event.CHANGE,dataChangeHandler);
		}
		/**
		 *销毁 
		 * 
		 */	
		public function destroyView():void
		{
			removeEvtListener();
			if(this.parent)
			{
				this.parent.removeChild(this);
			}
			for each(var item :ComGoodsItem in itemList)
			{
				item.destroyView();
				item = null;
			}
			itemList = null;
			DisplayObjectUtil.removeAllChildren(this);
			_symbolSkin = null;
			closeBtn = null;
		}
		/**
		 *显示tab类型 
		 * @param $obj
		 */
		public function showType($obj:Object):void
		{
		}
	}
}